class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


class Solution:
    def InsertLiast(self, head, val):
        temp = ListNode(val)
        while head.next != None:
            head = head.next
        head.next = temp

    def printList(self, head):
        while head != None:
            print(head.val, end=' ')
            head = head.next
        print()

    def reverseList(self, head: ListNode) -> ListNode:
        ans = None

        while head != None:
            temp=head
            head = head.next
            temp.next=ans
            ans=temp

            self.printList(ans)
        return ans



if __name__ == '__main__':
    head1 = ListNode(-1)

    s = Solution()
    for i in range(0, 10, 2):
        s.InsertLiast(head1, i)
    s.printList(head1)
    head1 = s.reverseList(head1)
    s.printList(head1)
